package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("DetectmethodDAO")
@Transactional
public class DetectmethodDAOImpl extends AbstractJpaDao<Detectmethod> implements DetectmethodDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Detectmethod.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public DetectmethodDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Detectmethod findDetectmethodById(Integer id) throws DataAccessException {

		return findDetectmethodById(id, -1, -1);
	}

	@Transactional
	public Detectmethod findDetectmethodById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectmethodById", startResult, maxRows, id);
			return (Detectmethod) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectmethod> findDetectmethodByName(String name) throws DataAccessException {

		return findDetectmethodByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectmethod> findDetectmethodByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectmethodByName", startResult, maxRows, name);
		return new LinkedHashSet<Detectmethod>(query.getResultList());
	}

	@Transactional
	public Set<Detectmethod> findDetectmethodByNameContaining(String name) throws DataAccessException {

		return findDetectmethodByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectmethod> findDetectmethodByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectmethodByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Detectmethod>(query.getResultList());
	}

	@Transactional
	public Set<Detectmethod> findDetectmethodByStep(String step) throws DataAccessException {

		return findDetectmethodByStep(step, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectmethod> findDetectmethodByStep(String step, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectmethodByStep", startResult, maxRows, step);
		return new LinkedHashSet<Detectmethod>(query.getResultList());
	}

	@Transactional
	public Set<Detectmethod> findDetectmethodByStepContaining(String step) throws DataAccessException {

		return findDetectmethodByStepContaining(step, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectmethod> findDetectmethodByStepContaining(String step, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectmethodByStepContaining", startResult, maxRows, step);
		return new LinkedHashSet<Detectmethod>(query.getResultList());
	}

	@Transactional
	public Set<Detectmethod> findDetectmethodByDescription(String description) throws DataAccessException {

		return findDetectmethodByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectmethod> findDetectmethodByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectmethodByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Detectmethod>(query.getResultList());
	}

	@Transactional
	public Set<Detectmethod> findDetectmethodByDescriptionContaining(String description) throws DataAccessException {

		return findDetectmethodByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectmethod> findDetectmethodByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectmethodByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Detectmethod>(query.getResultList());
	}

	@Transactional
	public Detectmethod findDetectmethodByPrimaryKey(Integer id) throws DataAccessException {
		return findDetectmethodByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Detectmethod findDetectmethodByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectmethodByPrimaryKey", startResult, maxRows, id);
			return (Detectmethod) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectmethod> findDetectmethodsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectmethodsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectmethod> findAllDetectmethods() throws DataAccessException {

		return findAllDetectmethods(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectmethod> findAllDetectmethods(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectmethods", startResult, maxRows);
		return new LinkedHashSet<Detectmethod>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectmethod> findDetectmethodByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Detectmethod>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Detectmethod> findDetectmethodsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Detectmethod.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Detectmethod> detectmethods = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return detectmethods;
	}

	@Transactional
	public Detectmethod findDetectmethodByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findDetectmethodByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Detectmethod findDetectmethodByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectmethodByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Detectmethod) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Detectmethod entity) {
		return true;
	}
}
